#!/bin/bash
# Generate skeleton files for completion of specified command.
# Test skeleton files are generated as well.
# @param $1 string  Command, e.g. 'make'
# @param $2 string  Completion function, e.g. _command
# @param $3 string  Completion arguments, e.g. ' -o filenames'


set -o errexit  # Exit if simple command fails


# Generate completion code
# @param $1 string  Command, e.g. 'make'
# @param $2 string  Completion function, e.g. _command
# @param $3 string  Completion arguments, e.g. ' -o filenames'
generate_completion() {
        # If `function' differs from _`command', set `comp_install' argument
    [ $2 = _$1 ] || local comp_install_arg=" -F $2"
	local path="completions/complete$3/$1"
        # If `command' contains dot (.), trail filename with dot (.)
    [ $1 = ${1/./} ] || path=$path. 
	echo "$path"
		# Does file already exist?
	if [ ! -f "$path" ]; then
		# No, file doesn't exist; generate file
	cat <<BASH > "$path"
$2()
{
} # $2()
BASH
    vi -c 'set paste' "$path"
fi
} # generate_completion()


# Generate test code
# @param $1 string  Command, e.g. 'make'
# @param $2 string  Completion function, e.g. _command
# @param $3 string  Completion arguments, e.g. ' -o filenames'
generate_test_completion_debian() {
	local path="test/completionDebian/$1.exp"
		# Does file already exist?
	if [ ! -f "$path" ]; then
		# No, file doesn't exist; generate file
	cat <<EXPECT > "$path"
source "lib/completions/$1.exp"
EXPECT
fi
} # generate_test_completion_debian()


# Generate test code
# @param $1 string  Command, e.g. 'make'
# @param $2 string  Completion function, e.g. _command
# @param $3 string  Completion arguments, e.g. ' -o filenames'
generate_test_completion_lib() {
	local path="test/completionLib/$1.exp"
		# Does file already exist?
	if [ ! -f "$path" ]; then
		# No, file doesn't exist; generate file
	cat <<EXPECT > "$path"
set test "Completion via comp_load() should be installed"
set cmd "complete -p $1"
send "\$cmd\\r"
expect {
    -re "^\$cmd\\r\\ncomplete$3 -F comp_load $1\\r\\n/@\$" { pass "\$test" }
    -re /@ { fail "\$test at prompt" }
}; # expect


source "lib/completions/$1.exp"


set test "Completion via $2() should be installed"
set cmd "complete -p $1"
send "\$cmd\\r"
expect {
    -re "^\$cmd\\r\\ncomplete$3 -F $2 $1\\r\\n/@\$" { pass "\$test" }
    -re /@ { fail "\$test at prompt" }
}; # expect


source "lib/completions/$1.exp"
EXPECT
fi
} # generate_test_completion_lib()


# Generate test code
# @param $1 string  Command, e.g. 'make'
# @param $2 string  Completion function, e.g. _command
# @param $3 string  Completion arguments, e.g. ' -o filenames'
generate_test_completion_orig() {
	local path="test/completionOrig/$1.exp"
		# Does file already exist?
	if [ ! -f "$path" ]; then
		# No, file doesn't exist; generate file
	cat <<EXPECT > "$path"
source "lib/completions/$1.exp"
EXPECT
fi
} # generate_test_completion_orig()


# Generate test code
# @param $1 string  Command, e.g. 'make'
# @param $2 string  Completion function, e.g. _command
# @param $3 string  Completion arguments, e.g. ' -o filenames'
generate_test_lib_completions() {
	local path="test/lib/completions/$1.exp"
		# Does file already exist?
	if [ ! -f "$path" ]; then
		# No, file doesn't exist; generate file
	cat <<EXPECT > "$path"
proc setup {} {
}; # setup()


proc teardown {} {
}; # teardown()


setup


set test "Tab should complete"
    # Try completion
set cmd "$1 "
send "\$cmd\\t"
expect {
    -re "\$cmd\r\n.*\$cmd\$" { pass "\$test" }
    -re /@ { unresolved "\$test at prompt" }
    -re eof { unresolved "eof" }
}; # expect


sync_after_int


teardown
EXPECT
fi
} # generate_test_lib_completions()


	# If argument count is wrong, show help
[ $# -eq 3 ] || { echo "Usage: $0 command function complete-options"; exit 1; }


generate_completion "$1" "$2" "$3"
generate_test_completion_debian "$1" "$2" "$3"
generate_test_completion_lib "$1" "$2" "$3"
generate_test_completion_orig "$1" "$2" "$3"
generate_test_lib_completions "$1" "$2" "$3"
